extends RefCounted


class_name EventCenter


## 挂载的所有效果
var effects: Array[EffectValue] = []
## 挂载的事件和处理器
var event_processors: Dictionary[CharacterUnit.Event, EventProcessor] = {}


func on_event(character: CharacterComponent, e: CharacterEvent) -> bool:
    var processor: EventProcessor = event_processors.get(e.type)
    if processor:
        return processor.do_event(character, e)
    # is_canceled = false
    return false

# 检测unique的效果是否和现有效果列表有冲突
func conflict(effect: EffectValue) -> Array:
    var conflict_effects: Array[EffectValue] = []
    for e_id in effect.support():
        if e_id == CharacterUnit.Event.NONE:
            break
        var processor: EventProcessor = event_processors.get(e_id)
        if processor and processor.unique and processor.unique.behavior != effect.behavior:
            conflict_effects.append(processor.unique)
    return conflict_effects

# 装载效果
func take(effect: EffectValue, is_only_merge: bool = false) -> void:
    var is_unique: bool = true
    for e_id in effect.behavior.support():
        # unique 和 multi 以 None 分隔
        if e_id == CharacterUnit.Event.NONE:
            is_unique = false
            continue
        var processor: EventProcessor = event_processors.get_or_add(e_id, EventProcessor.new())
        if is_unique:
            if processor.unique and processor.unique.behavior == effect.behavior:
                print("----->", processor.unique.value)
                processor.unique.value += effect.value
            elif not is_only_merge:
                processor.unique = effect
        else:
            var is_contains: = false
            for e in processor.effects:
                if e.behavior == effect.behavior:
                    is_contains = true
                    e.value += effect.value
                    break
            if not is_contains and not is_only_merge:
                processor.effects.append(effect.duplicate())
    effects.append(effect)

# 卸载效果
func drop(effect: EffectValue) -> void:
    var is_unique: bool = true
    for e_id in effect.behavior.support():
        # unique 和 multi 以 None 分隔
        if e_id == CharacterUnit.Event.NONE:
            is_unique = false
            continue
        var processor: EventProcessor = event_processors.get(e_id)
        if processor == null:
            continue
        if is_unique:
            if processor.unique and processor.unique.behavior == effect.behavior:
                if processor.unique.value > effect.value:
                    processor.unique.value -= effect.value
                else:
                    processor.unique = null
        else:
            var remove_indexs: Array[int] = []
            for i in range(len(processor.effects)):
                var e: EffectValue = processor.effects[i]
                if e.behavior == effect.behavior:
                    if e.value > effect.value:
                        e.value -= effect.value
                    else:
                        remove_indexs.append(i)
                    break
            for i in remove_indexs:
                processor.effects.pop_at(i)
    effects.erase(effect)

func drop_all() -> void:
    effects.clear()
    event_processors.clear()

func clone() -> EventCenter:
    var e: EventCenter = EventCenter.new()
    for e_id in event_processors:
        e.event_processors[e_id] = event_processors[e_id].clone()
    for effect in effects:
        e.effects.append(effect.duplicate())
    return e
